package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.productionPlan.dao.ProductionProcessDeviceDao;
import com.wing.productionPlan.model.entity.ProductionProcessDevice;
import com.wing.productionPlan.model.request.ProductionProcessDeviceForm;
import com.wing.productionPlan.model.response.ProductionProcessDeviceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * 生产工序设备表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductionProcessDeviceServiceImpl extends ServiceImpl<ProductionProcessDeviceDao, ProductionProcessDevice> implements ProductionProcessDeviceService {
    @Autowired
    private EnterpriseClient enterpriseClient;

    @Override
    public int add(ProductionProcessDeviceForm productionProcessDeviceForm) {
        //校验
        ProductionProcessDevice productionProcessDevice = new ProductionProcessDevice();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProductionProcessDevice.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productionProcessDeviceForm, productionProcessDevice, copyOptions);
        return baseMapper.insert(productionProcessDevice);
    }

    @Override
    public List<ProductionProcessDeviceVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductionProcessDeviceVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductionProcessDeviceVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductionProcessDeviceVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public Page<ProductionProcessDeviceVO> getProductionDeviceByProcessId(Page page, Long processId, String code, String deviceModel, String deviceName) {
        List<ProductionProcessDeviceVO> list = baseMapper.getProductionDeviceByProcessId(page, processId, code, deviceModel, deviceName);
        page.setRecords(list);
        for (ProductionProcessDeviceVO item : list) {
            //计算设备合格率
            if (item.getQualifiedQuantity().compareTo(BigDecimal.ZERO) == 1) {
                item.setYeild(item.getQualifiedQuantity().divide(item.getAmount(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            }
        }
        return page;
    }

    @Override
    public List<ProductionProcessDeviceVO> getProcessDeviceByProcessId(Long productionPlanId, Long productionPlanRecordId, Long processId) {
        List<ProductionProcessDeviceVO> deviceVOList = baseMapper.getProcessDeviceByProcessId(productionPlanId, productionPlanRecordId, processId);
        for (ProductionProcessDeviceVO item : deviceVOList) {
            //计算设备合格率
            if (item.getQualifiedQuantity().compareTo(BigDecimal.ZERO) == 1) {
                item.setYeild(item.getQualifiedQuantity().divide(item.getAmount(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            }
        }
        return deviceVOList;
    }

    @Override
    public Integer getSumProductionQuantity(Long processId, Long productionPlanId) {
        return baseMapper.getSumProductionQuantity(processId, productionPlanId);
    }

}
